// 导出页面为PDF格式
import html2Canvas from 'html2canvas'
import JsPDF from 'jspdf'
const htmlToPdfLoopOut = {
	getPdf: (obj) => {
		return new Promise((resolve, reject) => {
			let title = `${obj.match_name}-${obj.project_name}`
			let get_imgObj = async function() {
				if (obj.projectType == 3 || obj.projectType == 2 || obj.projectType == 5) return ''
				let headers = await htmlToPdfLoopOut.getpdf_headers_imgObj(obj)
				let secondStage = await htmlToPdfLoopOut.getpdf_secondStage_imgObj(obj)
				let repechage = obj.repechage ? await htmlToPdfLoopOut.getpdf_repechage_imgObj(obj) :''
				let imgObj = {
					headers: headers,
					secondStage: secondStage,
					repechage: repechage
				}
				return imgObj
			}
			get_imgObj().then((imgObj) => {
				console.log(imgObj)
				// htmlToPdf.getpdf_secondStage_imgObj(obj).then((secondStage_imgObj)=>{
				//console.log(headers_imgObj)
				html2Canvas(document.querySelector('#pdfDom'), {
					async: true,
					allowTaint: true,
					// width:1600,
					scale: 1.5,
				}).then(function(canvasObj) {
					let contentWidth = canvasObj.width
					//console.log(canvasObj.width)
					let contentHeight = canvasObj.height
					//一页pdf显示html页面生成的canvas高度;
					let pageHeight = contentWidth / 592.28 * 841.89
					let pageCount = 0
					//未生成pdf的html页面高度
					let leftHeight = contentHeight
					let leftWidth = obj.nums == 0 ? 0 : 16 //32 16 8
					//页面偏移
					let position = 0
					//a4纸的尺寸[595.28,841.89]，html页面生成的canvas在pdf中图片的宽高
					let imgWidth = 595.28
					console.log(obj.projectType)
					let imgHeight = 592.28 / contentWidth * contentHeight
					console.log(imgHeight)
					let pageData = canvasObj.toDataURL('image/jpeg', 1.0)
					// console.log(pageData)
					//转img 2 pdf
					let PDF = new JsPDF('', 'pt', 'a4')
					//有两个高度需要区分，一个是html页面的实际高度，和生成pdf的页面高度(841.89)
					//当内容未超过pdf一页显示的范围，无需分页
					if (leftHeight < pageHeight) {
						// console.log(pageData)
						PDF.addImage(pageData, 'JPEG', 0, 16, imgWidth, imgHeight)
					} else {
						while (leftHeight > 0) {
							if (pageCount > 0) { //大于第一页
								PDF.addImage(pageData, 'JPEG', leftWidth, position, imgWidth, imgHeight)
								leftHeight -= pageHeight
								position -= 841.89
								pageCount++
								//避免添加空白页
								if (leftHeight > 0) {
									PDF.addPage()
								}
							} else {
								// imgHeight = 592.28 / contentWidth * contentHeight
								PDF.addImage(pageData, 'JPEG', leftWidth, position, imgWidth, imgHeight)
								leftHeight -= pageHeight
								position -= 841.89
								pageCount++
								//避免添加空白页
								if (leftHeight > 0) {
									PDF.addPage()
								}
							}
						}
					}
					//测试成功
					if (imgObj) { //1 4赛制
						PDF.addPage();
						//高度可能在组件表里或者在result里
						PDF.addImage(imgObj.headers.imgObj, 'JPEG', leftWidth, 36, imgObj.headers.width, imgObj.headers.height);
						PDF.addImage(imgObj.secondStage.imgObj, 'JPEG', leftWidth, 50 + imgObj.headers.height, imgObj.secondStage
							.width, imgObj.secondStage.height);
							// 新增淘汰赛
						if (obj.repechage) {
							PDF.addPage();
							PDF.addImage(imgObj.headers.imgObj, 'JPEG', 0, 16, imgObj.headers.width, imgObj.headers.height);
							PDF.addImage(imgObj.repechage.imgObj, 'JPEG', 0, 50 + imgObj.headers.height, imgObj.repechage.width, imgObj.repechage.height);
						}
						// PDF.addImage(headers_imgObj.imgObj,'JPEG', leftWidth, 0, headers_imgObj.width, headers_imgObj.height);
						// PDF.addImage(pageData, 'JPEG', leftWidth, headers_imgObj.height, imgWidth-headers_imgObj.width, imgHeight- headers_imgObj.height) 这样才是叠加
					}
					PDF.save(title + '.pdf')
					resolve()
				})
				// })
			})
		})
	},
	//测试用
	getpdf_headers_imgObj(obj) {
		return new Promise((resolve, reject) => {
			html2Canvas(document.querySelector('#pdfDomHeaders'), {
				allowTaint: true
			}).then(function(canvasObj) {
				let contentWidth = canvasObj.width
				let contentHeight = canvasObj.height
				let imgWidth = 595.28
				let imgHeight = 592.28 / contentWidth * contentHeight
				let pageData = canvasObj.toDataURL('image/jpeg', 5.0)
				//转img 2 pdf
				let obj = {
					imgObj: pageData,
					width: imgWidth,
					height: imgHeight
				}
				resolve(obj)
			})
		})
	},
	getpdf_secondStage_imgObj(obj) {
		return new Promise((resolve, reject) => {
			html2Canvas(document.querySelector('#pdfDomSecondStage'), {
				allowTaint: true
			}).then(function(canvasObj) {
				let contentWidth = canvasObj.width
				let contentHeight = canvasObj.height
				let imgWidth = 595.28
				let imgHeight = 592.28 / contentWidth * contentHeight
				let pageData = canvasObj.toDataURL('image/jpeg', 5.0)
				//转img 2 pdf
				let obj = {
					imgObj: pageData,
					width: imgWidth,
					height: imgHeight
				}
				resolve(obj)
			})
		})
	},
	getpdf_repechage_imgObj(obj) {
		return new Promise((resolve, reject) => {
			html2Canvas(document.querySelector('#repechage'), {
				allowTaint: true
			}).then(function(canvasObj) {
				let contentWidth = canvasObj.width
				let contentHeight = canvasObj.height
				let imgWidth = 595.28
				let imgHeight = 592.28 / contentWidth * contentHeight
				let pageData = canvasObj.toDataURL('image/jpeg', 5.0)
				//转img 2 pdf
				let obj = {
					imgObj: pageData,
					width: imgWidth,
					height: imgHeight
				}
				resolve(obj)
			})
		})
	},
}
export default htmlToPdfLoopOut
